﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;
using Sita.Data.Core;

namespace Sita.Data.Repositories
{
    public class InvoiceRepository : GenericRepository<Invoice>
    {
        public InvoiceRepository() :
            base(new SitaContext())
        { }

        public IList<Invoice> GetPaginatedByBusinessUnitID(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, out int totalRows)
        {
            RepositoryHelper<Invoice> Helper = new RepositoryHelper<Invoice>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Invoice> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).Invoices
                .Where(i => i.BusinessUnitID == businessUnitID)
                .Select(i => i);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Invoice>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Invoice>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList();

        }

        public Invoice GetByIDWithItemsAndCustomer(int invoiceID)
        {
            Invoice ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Invoices
                .Where(i => i.InvoiceID == invoiceID)
                .Include("InvoiceReplacements")
                .Include("InvoiceReplacements.Replacement")
                .Include("InvoiceExtraItems")
                .Include("InvoiceDeliveryNotes")
                .Include("InvoiceDeliveryNotes.DeliveryNote")
                .Include("InvoiceDeliveryNotes.DeliveryNote.DeliveryNoteReplacements")
                .Include("InvoiceDeliveryNotes.DeliveryNote.DeliveryNoteReplacements.Replacement")
                .Include("Customer")
                .Include("BusinessUnit")
                .SingleOrDefault();

            return ReturnSet;
        }

        public Invoice GetByIDWithItems(int invoiceID)
        {
            Invoice ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Invoices
                .Where(i => i.InvoiceID == invoiceID)
                .Include("InvoiceReplacements")
                .Include("InvoiceExtraItems")
                .Include("InvoiceDeliveryNotes")                
                .SingleOrDefault();

            return ReturnSet;
        }
    }
}
